package com.bw.mallshop.mvvm.viewmodel;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.adapter.ConfirmOrderGoodsAdapter;
import com.bw.mallshop.dao.UserSingle;
import com.bw.mallshop.entity.CarListEntity;
import com.bw.mallshop.entity.Order;
import com.bw.mallshop.entity.OrderEntity;
import com.bw.mallshop.entity.OrderGoods;
import com.bw.mallshop.entity.PayEntity;
import com.bw.mallshop.entity.PayOrderEntity;
import com.bw.mallshop.entity.ShipAddress;
import com.bw.mallshop.entity.SubOrderEntity;
import com.bw.mallshop.mvvm.model.ConfirmOrderModel;
import com.bw.mallshop.mvvm.view.ShipAddressListActivity;
import com.bw.mallshop.pay.AliPayActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ConfirmOrderViewModel extends BaseViewModel<ConfirmOrderModel>
        implements View.OnClickListener, OnItemClickListener {

    public ObservableField<String>totalPrice = new ObservableField<>("￥0");
    public ObservableField<String>shipname = new ObservableField<>("zxy");
    public ObservableField<String>shipaddress = new ObservableField<>("bw");
    public ObservableField<Boolean>shipflag = new ObservableField<>(true);
    public ConfirmOrderGoodsAdapter adapter = new ConfirmOrderGoodsAdapter();
    private List<OrderGoods> orderGoodsList=new ArrayList<>();
    public int orderId;
    public ArrayList<CarListEntity.Values> listEntities;
    private OrderEntity orderEntity;

    @Override
    protected void initData() {
        //根据订单id查询订单详情
        Map<String,Object>map=new HashMap<>();
        map.put("orderId",orderId);
        m.requestOrderDetails(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof OrderEntity){
            orderEntity = (OrderEntity) entity;
            //列表赋值
            adapter.setNewInstance(orderEntity.data.orderGoodsList);
            totalPrice.set("￥"+orderEntity.data.totalPrice);
        }else if (entity instanceof SubOrderEntity){
            SubOrderEntity subOrderEntity = (SubOrderEntity) entity;
            if (subOrderEntity.message.equals("订单提交成功")){
                //用户确认完成订单成功->订单进入待支付状态->获取支付宝支付信息(服务器接口),去支付
                getPaySign();
                return;
            }
            showErro(subOrderEntity.message);
        }else if (entity instanceof PayEntity){
            PayEntity payEntity = (PayEntity) entity;
            if (!TextUtils.isEmpty(payEntity.data)){
                Bundle bundle = new Bundle();
                bundle.putString("alipayStr",payEntity.data);
                startPage(AliPayActivity.class,bundle);
                return;
            }
            showErro("支付信息获取失败");
        }else if (entity instanceof PayOrderEntity){
            PayOrderEntity payOrderEntity = (PayOrderEntity) entity;
            if (payOrderEntity.message.equals("订单支付成功")){
                finishPage();
                //刷新购物车
            }
        }
    }

    //获取支付签名以及orderInfo支付必要信息
    private void getPaySign(){
        Map<String,Object>map=new HashMap<>();
        map.put("orderId",orderEntity.data.id);
        map.put("totalPrice",orderEntity.data.totalPrice);
        m.requestGetPay(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void finish(){
        finishPage();
    }

    public void subOrderListener(){
        //用户确认订单->订单状态从待确认->待付款
        /**
         * {
         *   "order": {
         *     "id": 0,
         *     "orderGoodsList": [
         *       {
         *         "goods_count": 0,
         *         "goods_desc": "string",
         *         "goods_icon": "string",
         *         "goods_id": 0,
         *         "goods_price": "string",
         *         "goods_sku": "string",
         *         "id": 0,
         *         "order_id": 0
         *       }
         *     ],
         *     "orderStatus": 0,
         *     "payType": 0,
         *     "shipAddress": {
         *       "id": 0,
         *       "ship_address": "string",
         *       "ship_is_default": 0,
         *       "ship_user_mobile": "string",
         *       "ship_user_name": "string",
         *       "user_id": 0
         *     },
         *     "totalPrice": "string"
         *   },
         *   "userId": 0
         * }
         * */
        ShipAddress shipAddress = new ShipAddress();
        shipAddress.id = 9;
        shipAddress.ship_is_default = 1;
        shipAddress.ship_user_name = "11";
        shipAddress.ship_address = "11";
        shipAddress.ship_user_mobile = "11";
        shipAddress.user_id = 481;
        Map<String,Object>map=new HashMap<>();
        map.put("userId", UserSingle.getInstance().userEntityValues.id);
        Order order = new Order();
        order.totalPrice = orderEntity.data.totalPrice;
        order.orderStatus = 0;//待支付状态
        order.payType = 0;//支付宝支付
        order.id = orderEntity.data.id;
        order.orderGoodsList = orderEntity.data.orderGoodsList;
        order.shipAddress = shipAddress;
        map.put("order",order);
        m.requestConfirmOrder(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    //校验当前订单是否收到钱方法
    public void checkOrder(){
        Map<String,Object>map=new HashMap<>();
        map.put("orderId",orderEntity.data.id);
        m.requestConfirmPay(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

   public void refreshShip(){

   }

    public void shipclick(){
        startPage(ShipAddressListActivity.class);
    }

    @Override
    public void onClick(View v) {
         startPage(ShipAddressListActivity.class);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

    }
}
